package org.cem.security;

import org.cem.datasource.DbAccess;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.security.auth.ILoginContext;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;

/**
 * The activator class controls the plug-in life cycle
 */
public class CemSecurityPlugin extends AbstractUIPlugin {

	public static final String PLUGIN_ID = "RdbmSecurityModule"; //$NON-NLS-1$

	private static CemSecurityPlugin plugin;

	private BundleContext context;

	private ServiceRegistration<?> userAdminListenerRegistration;
	private RapLoginContext loginContext;
	private ServiceRegistration<?> loginContextRegistration;

	private ServiceTracker conditionalPermissionAdminTracker;
	private ServiceTracker loginContextTracker;
	
	/**
	 * The constructor
	 */
	public CemSecurityPlugin() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		this.context = context;

		this.loginContext = new RapLoginContext("RDBMS", getBundle().getEntry(
				"jaas_config.txt"));
		this.loginContextRegistration = context.registerService(
				ILoginContext.class.getName(), loginContext, null);
		this.loginContextTracker = new ServiceTracker(context, ILoginContext.class
				.getName(), null);
		
		loginContextTracker.open();
		
		DbAccess.initializeDataBase();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		userAdminListenerRegistration.unregister();
		loginContextRegistration.unregister();

		this.conditionalPermissionAdminTracker.close();
		this.loginContextTracker.close();

		plugin = null;
		this.context = null;

		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static CemSecurityPlugin getDefault() {
		return plugin;
	}
	
	public static ILoginContext getLoginContext() {
		return (ILoginContext) getDefault().loginContextTracker.getService();
	}

	public static void log(IStatus status) {
		getDefault().getLog().log(status);
	}

	public static void logErrorMessage(String message) {
		log(new Status(IStatus.ERROR, PLUGIN_ID,
				101, message, null));
	}

	public static void logErrorStatus(String message, IStatus status) {
		if (status == null) {
			logErrorMessage(message);
			return;
		}
		MultiStatus multi = new MultiStatus(PLUGIN_ID,
				101, message, null);
		multi.add(status);
		log(multi);
	}

	public static void log(Throwable e) {
		log(new Status(IStatus.ERROR, PLUGIN_ID,
				101,
				"error", e));
//		UserAdminUiMessages.get().UserAdminUiPlugin_internal_error<>
	}
	
}
